from flask import render_template, request, redirect, url_for
from flask_login import login_user, login_required, logout_user

from . import auth

from models import User, users


@auth.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        print(username)
        password = request.form['password']
        print(password)
        if username in users and users[username][username] == password:
            user = User(username)
            login_user(user)
            return redirect(url_for('menu.index'))
        return 'Invalid username or password'
    return render_template('login.html')


@auth.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('menu.index'))
